require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'

class TC_ForumSettings_test < Test::Unit::TestCase
   
    def startup
      $Allowed ='zip;cab;jpg;gif;png;mpg;mpeg;avi;wmv;wma;mp3;ra;rar;rm;sql;txt'
      $Width ='-2'
      $Height='555'
      $Image ='jpg;gif;bmp;pcx;png;pic'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
	  def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
    def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
    end
    
    def test_06_verifyControlPanel
        assert(ie.link(:text, /Control Panel/).exists?)
        ie.link(:text, /Control Panel/).click
        assert(ie.contains_text(/Announcements/))
        assert(ie.link(:text, /System Administration/).exists?)
        ie.link(:text, /System Administration/).click
        assert(ie.contains_text(/Announcements/))
    end
    
    def test_07_verifyControlPanelLinks
        assert_equal $SITE_URL + 'controlpanel/', ie.link(:text, 'Dashboard').href
        assert_equal $SITE_URL + 'controlpanel/setup.aspx', ie.link(:text, 'Administration').href
        assert_equal $SITE_URL + 'controlpanel/BlogAdmin/', ie.link(:text, 'Blog Administration').href
        assert_equal $SITE_URL + 'controlpanel/MediaGalleryAdmin/', ie.link(:text, 'Media Galleries Administration').href
        assert_equal $SITE_URL + 'controlpanel/Forums/', ie.link(:text, 'Forum Administration').href
        assert_equal $SITE_URL + 'controlpanel/HubAdmin/', ie.link(:text, 'Group Administration').href
        assert_equal $SITE_URL + 'controlpanel/Membership/membersearch.aspx?SelectedNavItem=BrowseMembers', ie.link(:text, 'Membership Administration').href
        assert_equal $SITE_URL + 'controlpanel/Settings/default.aspx', ie.link(:text, 'Site Administration').href
    end
    
    def test_08_navigateToMediaGalleryAdministration
        ie.goto($SITE_URL + 'controlpanel/forums/')
        assert(ie.contains_text('Forum Administration'))      
    end  
    def test_09_verifyLinksExist
      # Make sure that all of the links exist
        assert(ie.link(:text, 'Log Out').exists?)
        assert(ie.link(:text, 'Help').exists?)
        assert(ie.link(:text, 'Go to Site').exists?)
        assert(ie.link(:text, 'Dashboard').exists?)
        assert(ie.link(:text, 'Administration').exists?)
        assert(ie.link(:text, 'Forum Administration').exists?)
        assert(ie.link(:text, 'Telligent').exists?)
    end
    def test_10_navigateToGeneralForumSettings
        ie.goto($SITE_URL +'controlpanel/forums/options/PostOptions.aspx')
        assert(ie.radio(:id,/EnableForumsRSS_0$/).exists?)
        assert(ie.radio(:id,/EnableForumsRSS_1$/).exists?)
        ie.radio(:id,/EnableForumsRSS_1$/).set
        assert(ie.radio(:id,/EnableThreadStatus_0$/).exists?)
        assert(ie.radio(:id,/EnableThreadStatus_1$/).exists?)
        ie.radio(:id,/EnableThreadStatus_0$/).set
        assert(ie.radio(:id,/EnablePostPreviewPopup_0$/).exists?)
        assert(ie.radio(:id,/EnablePostPreviewPopup_1$/).exists?)
        ie.radio(:id,/EnablePostPreviewPopup_0$/).set
        assert(ie.radio(:id,/optEnableUserModerationCounters_0$/).exists?)
        assert(ie.radio(:id,/optEnableUserModerationCounters_1$/).exists?)
        ie.radio(:id,/optEnableUserModerationCounters_0$/).set
        assert(ie.radio(:id,/PostAsAnonymousUser_0$/).exists?)
        assert(ie.radio(:id,/PostAsAnonymousUser_1$/).exists?)
        ie.radio(:id,/PostAsAnonymousUser_0$/).set
        assert(ie.radio(:id,/Tagging_0$/).exists?)
        assert(ie.radio(:id,/Tagging_1$/).exists?)
        ie.radio(:id,/Tagging_1$/).set
        sleep 1
        assert(ie.text_field(:id,/RSSDefaultThreadsPerFeed$/).exists?)
        sleep 1
        ie.text_field(:id,/RSSDefaultThreadsPerFeed$/).set('30')
        sleep 1
        assert(ie.text_field(:id,/ThreadsPerPage$/).exists?)
        sleep 1
        ie.text_field(:id,/ThreadsPerPage$/).set('25')
        sleep 1
        assert(ie.text_field(:id,/PostsPerPage$/).exists?)
        sleep 1
        ie.text_field(:id,/PostsPerPage$/).set('18')
        sleep 1
        assert(ie.text_field(:id,/DaysPostMarkedAsRead$/).exists?)
        sleep 1
        ie.text_field(:id,/DaysPostMarkedAsRead$/).set('8')
        sleep 1
        assert(ie.select_list(:name,/ctl00$/).exists?)
        sleep 1
        ie.select_list(:name,/ctl00$/).select('Newer than 6 Months')
      end 
    def test_11_navigateToEditingForumSettings
        ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.GetTabAtIndex(1))")
        assert(ie.radio(:id,/RequireEditNotes_0$/).exists?)
        assert(ie.radio(:id,/RequireEditNotes_1$/).exists?)
        ie.radio(:id,/RequireEditNotes_0$/).set
        assert(ie.radio(:id,/DisplayEditNotes_0$/).exists?)
        assert(ie.radio(:id,/DisplayEditNotes_1$/).exists?)
        ie.radio(:id,/DisplayEditNotes_0$/).set
        sleep 1
        assert(ie.text_field(:id,/PostEditBodyAgeInMinutes$/).exists?)
        sleep 1
        ie.text_field(:id,/PostEditBodyAgeInMinutes$/).set('5')
    end
    def test_12_navigateToAttachmentSettings
      ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.GetTabAtIndex(2))")
      assert(ie.radio(:id,/EnableAttachments_0$/).exists?)
      assert(ie.radio(:id,/EnableAttachments_0$/).exists?)
      ie.radio(:id,/EnableAttachments_0$/).set
      sleep 3
      assert(ie.text_field(:id,/AllowedAttachmentTypes$/).exists?)
      sleep 2
      ie.text_field(:id,/AllowedAttachmentTypes$/).set($Allowed)
      sleep 2
      assert(ie.text_field(:id,/MaxAttachmentSize$/).exists?)
      sleep 2
      ie.text_field(:id,/MaxAttachmentSize$/).set('64')
      assert(ie.radio(:id,/EnableInlinedImages_0$/).exists?)
      assert(ie.radio(:id,/EnableInlinedImages_1$/).exists?)
      ie.radio(:id,/EnableInlinedImages_0$/).set
      sleep 2
      assert(ie.text_field(:id,/InlinedImageWidth$/).exists?)
      sleep 2
      ie.text_field(:id,/InlinedImageWidth$/).set($Width)
      sleep 2
      assert(ie.text_field(:id,/InlinedImageHeight$/).exists?)
      sleep 2
      ie.text_field(:id,/InlinedImageHeight$/).set($Height)
      sleep 2
      assert(ie.text_field(:id,/SupportedInlinedImageTypes$/).exists?)
      sleep 2
      ie.text_field(:id,/SupportedInlinedImageTypes$/).set($Image)
    end
    def test_13_navigateToDuplicatesFlooding
       ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.GetTabAtIndex(3))")
       assert(ie.radio(:id,/AllowDuplicatePosts_0$/).exists?)
       assert(ie.radio(:id,/AllowDuplicatePosts_0$/).exists?)
       ie.radio(:id,/AllowDuplicatePosts_0$/).set
       sleep 2
       assert(ie.text_field(:id,/DuplicatePostIntervalInMinutes$/).exists?)
       sleep 2
       ie.text_field(:id,/DuplicatePostIntervalInMinutes$/).set('24')
       assert(ie.radio(:id,/EnableFloodInterval_0$/).exists?)
       assert(ie.radio(:id,/EnableFloodInterval_0$/).exists?)
       ie.radio(:id,/EnableFloodInterval_0$/).set
       sleep 2
       assert(ie.text_field(:id,/PostInterval$/).exists?)
       sleep 2
       ie.text_field(:id,/PostInterval$/).set('25')
     end
     
    def test_14_navigateToPopularPost 
      ie.ie.Document.parentWindow.execScript("ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.SelectTab(ctl00_ctl00_ctl00_OuterTaskRegion_TaskRegion_TaskRegion_TaskRegion_EditorTabs.GetTabAtIndex(4))")
      assert(ie.text_field(:id,/PopularPostThresholdPosts$/).exists?)
      sleep 2
      ie.text_field(:id,/PopularPostThresholdPosts$/).set('25')
      sleep 2
      assert(ie.text_field(:id,/PopularPostThresholdViews$/).exists?)
      sleep 2
      ie.text_field(:id,/PopularPostThresholdViews$/).set('225')
      sleep 2
      assert(ie.text_field(:id,/PopularPostThresholdDays$/).exists?)
      sleep 2
      ie.text_field(:id,/PopularPostThresholdDays$/).set('3')
    end    
    
    def test_15_Save
        assert(ie.link(:id,/SaveButton$/).exists?)
        #ie.link(:id,/SaveButton$/).click
      end
      
    def test_99_Exit
        ie.close
    end
  
end     
        
        